package database

import (
	"alfred/models"
	"time"
)

// CreateFlowExecution 创建FlowExecution
func (db *DB) CreateFlowExecution(execution *models.FlowExecution) error {
	execution.ID = models.NewID()
	execution.StartTime = time.Now()
	if execution.Status == "" {
		execution.Status = "running"
	}

	return db.Create(execution).Error
}

// GetFlowExecution 获取FlowExecution
func (db *DB) GetFlowExecution(id string) (*models.FlowExecution, error) {
	var execution models.FlowExecution
	err := db.First(&execution, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &execution, nil
}

// GetFlowExecutionsByFlowID 根据FlowID获取所有FlowExecution
func (db *DB) GetFlowExecutionsByFlowID(flowID string) ([]*models.FlowExecution, error) {
	var executions []*models.FlowExecution
	err := db.Where("flow_id = ?", flowID).Order("start_time DESC").Find(&executions).Error
	return executions, err
}

// ListFlowExecutions 获取所有FlowExecution
func (db *DB) ListFlowExecutions() ([]*models.FlowExecution, error) {
	var executions []*models.FlowExecution
	err := db.Order("start_time DESC").Find(&executions).Error
	return executions, err
}

// UpdateFlowExecution 更新FlowExecution
func (db *DB) UpdateFlowExecution(execution *models.FlowExecution) error {
	return db.Save(execution).Error
}

// DeleteFlowExecution 删除FlowExecution
func (db *DB) DeleteFlowExecution(id string) error {
	return db.Delete(&models.FlowExecution{}, "id = ?", id).Error
}

// CreateNodeExecution 创建NodeExecution
func (db *DB) CreateNodeExecution(execution *models.NodeExecution) error {
	execution.ID = models.NewID()
	execution.StartTime = time.Now()
	if execution.Status == "" {
		execution.Status = "running"
	}

	return db.Create(execution).Error
}

// GetNodeExecution 获取NodeExecution
func (db *DB) GetNodeExecution(id string) (*models.NodeExecution, error) {
	var execution models.NodeExecution
	err := db.First(&execution, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &execution, nil
}

// GetNodeExecutionsByFlowExecutionID 根据FlowExecutionID获取所有NodeExecution
func (db *DB) GetNodeExecutionsByFlowExecutionID(flowExecutionID string) ([]*models.NodeExecution, error) {
	var executions []*models.NodeExecution
	err := db.Where("flow_execution_id = ?", flowExecutionID).Order("start_time").Find(&executions).Error
	return executions, err
}

// ListNodeExecutions 获取所有NodeExecution
func (db *DB) ListNodeExecutions() ([]*models.NodeExecution, error) {
	var executions []*models.NodeExecution
	err := db.Order("start_time DESC").Find(&executions).Error
	return executions, err
}

// UpdateNodeExecution 更新NodeExecution
func (db *DB) UpdateNodeExecution(execution *models.NodeExecution) error {
	return db.Save(execution).Error
}

// DeleteNodeExecution 删除NodeExecution
func (db *DB) DeleteNodeExecution(id string) error {
	return db.Delete(&models.NodeExecution{}, "id = ?", id).Error
}

// CreateActionExecution 创建ActionExecution
func (db *DB) CreateActionExecution(execution *models.ActionExecution) error {
	execution.ID = models.NewID()
	execution.StartTime = time.Now()
	if execution.Status == "" {
		execution.Status = "running"
	}

	return db.Create(execution).Error
}

// GetActionExecution 获取ActionExecution
func (db *DB) GetActionExecution(id string) (*models.ActionExecution, error) {
	var execution models.ActionExecution
	err := db.First(&execution, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &execution, nil
}

// GetActionExecutionsByNodeExecutionID 根据NodeExecutionID获取所有ActionExecution
func (db *DB) GetActionExecutionsByNodeExecutionID(nodeExecutionID string) ([]*models.ActionExecution, error) {
	var executions []*models.ActionExecution
	err := db.Where("node_execution_id = ?", nodeExecutionID).Order("start_time").Find(&executions).Error
	return executions, err
}

// ListActionExecutions 获取所有ActionExecution
func (db *DB) ListActionExecutions() ([]*models.ActionExecution, error) {
	var executions []*models.ActionExecution
	err := db.Order("start_time DESC").Find(&executions).Error
	return executions, err
}

// UpdateActionExecution 更新ActionExecution
func (db *DB) UpdateActionExecution(execution *models.ActionExecution) error {
	return db.Save(execution).Error
}

// DeleteActionExecution 删除ActionExecution
func (db *DB) DeleteActionExecution(id string) error {
	return db.Delete(&models.ActionExecution{}, "id = ?", id).Error
}

// GetActionExecutions 获取所有ActionExecution (兼容旧API)
func (db *DB) GetActionExecutions() ([]*models.ActionExecution, error) {
	return db.ListActionExecutions()
}
